//
//  UISnapViewController.swift
//  Animator
//
//  Created by liu guoqiang on 16/9/9.
//  Copyright © 2016年 liu guoqiang. All rights reserved.
//

import UIKit

class UISnapViewController: UIViewController {
    var animator : UIDynamicAnimator!
    var snap : UISnapBehavior!

    @IBOutlet weak var box: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        box.userInteractionEnabled = true;
        box.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(UISnapViewController.snapClick(_:))))
          self.title = "甩行为"
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.animator = UIDynamicAnimator(referenceView: self.view)
       
    }

    
 func snapClick(sender: AnyObject) {
        print("甩")
        let gesture = sender as! UITapGestureRecognizer
        let point = gesture.locationInView(self.view)
        if self.snap != nil {
            self.animator.removeBehavior(self.snap)
        }
        self.snap = UISnapBehavior(item: self.box, snapToPoint: point)
        self.animator.addBehavior(self.snap)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
